﻿using Spectre.Console;
using System.Security.Cryptography;
using System.Text;

namespace WorkTool.Helpers
{
    public static class ToolHelper
    {
        /// <summary>
        /// 拆分符号
        /// </summary>
        public const string SPLIT_STRING = "#####";

        /// <summary>
        /// 默认保存地址
        /// </summary>
        private static readonly string _saveFolder = Path.Combine(Directory.GetCurrentDirectory(), "WorkTool");

        /// <summary>
        /// 月份转换
        /// </summary>
        public static readonly Dictionary<string, string> _month = new()
        {
            {"01","Jan"},
            {"02","Feb"},
            { "03", "Mar"},
            { "04", "Apr"},
            { "05", "May"},
            { "06", "Jun"},
            { "07", "Jul"},
            { "08", "Aug"},
            { "09", "Sep"},
            { "10", "Oct"},
            { "11", "Nov"},
            { "12", "Dec"}
        };

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="saveFolder">文件保存地址</param>
        public static string CreateDirectory(string saveFolder)
        {
            if (string.IsNullOrWhiteSpace(saveFolder))
                saveFolder = _saveFolder;
            try
            {
                DirectoryInfo directory = Directory.CreateDirectory(saveFolder);
            }
            catch (Exception ex) when (ex is UnauthorizedAccessException ||
                                      ex is PathTooLongException ||
                                      ex is IOException)
            {
                throw;
            }
            return saveFolder;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string CalculateMD5(this string input)
        {
            byte[] hashBytes = MD5.HashData(Encoding.UTF8.GetBytes(input));
            return string.Concat(hashBytes.Select(b => b.ToString("x2")));
        }

        /// <summary>
        /// URL编码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string EscapeDataString(this string input)
        {
            return Uri.EscapeDataString(input).Replace("%2F", "/"); ;
        }

        private static Dictionary<int, string> _reserveConsole = [];

        public static void ReserveConsole(int index, string consoleMessage)
        {
            if (!_reserveConsole.TryAdd(index, consoleMessage))
            {
                _reserveConsole[index] = consoleMessage;
            }
        }

        public static void ConsoleClear()
        {
            // Console.Write("\x1B[2K"); // 清除整行
            // Console.Write("\x1B[J"); // 清除从光标到屏幕末尾
            AnsiConsole.Clear();
            Console.SetCursorPosition(0, 0);
            foreach (var reserveConsole in _reserveConsole)
            {
                AnsiConsole.MarkupLine(reserveConsole.Value);
            }
            Console.SetCursorPosition(0, _reserveConsole.Keys.Max() + 1);
        }
    }

    public class FileSettings
    {
        /// <summary>
        /// 文件保存地址，默认保存在项目地址
        /// </summary>
        public string SaveFolder { get; set; }

        /// <summary>
        /// 文件名是否包含随机数
        /// </summary>
        public bool SaveFileNameRandom { get; set; }
    }
}